<?php
/**
 * Created by PhpStorm.
 * User: iiinsomnia
 * Date: 2017/11/25
 * Time: 17:39
 */
namespace App\Service;

use App\Models\User;
use Illuminate\Container\Container;
use Laravel\Lumen\Application;
use Illuminate\Http\Request;

class UserService {

    private static $_user;

    public function add($input) {
        $model = new User();

        $token = md5($input['wechat_id'] . time());

        $model->name = $input['name'];
        $model->wechat_id = $input['wechat_id'];
        $model->head_img_url = $input['head_img_url'];
        $model->token = $token;
        $model->created_at = date('Y-m-d H:i:s');
        $model->updated_at = date('Y-m-d H:i:s');

        if (!$model->save()) {
            return false;
        }

        return $token;
    }


    /**
     * @return User;
     */
    public static function authUser(){
        return app('loginUser');
        if(self::$_user){
            return self::$_user;
        }
        $token = Request::capture()->header('access-token');
        self::$_user = User::select('id', 'name', 'head_img_url')->where('token', $token)->first();
        return self::$_user;
    }
}
